import { createRouter, createWebHashHistory,createWebHistory, RouteRecordRaw } from 'vue-router'
import { staticRoutes } from './constantRoutes'


const router = createRouter({
  history: createWebHistory(),
  routes: staticRoutes as RouteRecordRaw[],
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
})

//定义前置路由守卫
//1. 导入userStore
import userStore from '@/store/modules/login/userStore'
//2. 前置守卫
router.beforeEach((to,from,next)=>{
  //获取token
  let user = userStore();
  let token = user.token;
  console.log("-------------"+token);
  if(token){
    //已经登录
    if(to.path == "/login"){
      //提示: 不让去
      next("/"); //首页
    }else{
      next();
    }
  }else{
    //没有登录
    if(to.path == "/login"){
      next();
    }else{
      //提示: 不能去
      next("/login");
    }
  }
})


export default router
